<?php
namespace PsiFramework\Applications\AlloMediaAdmin\Classes\Actions\CMS;

import("Classes.Actions.AbstractAction");
import("Classes.Factory.DAOFactory");
import("Classes.Util.Manager.ObjectManager");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Manager\ObjectManager;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action d'édition
 *
 * @package Actions
 */
class EditAction extends AbstractAction
{

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     */
    public function execute()
    {
        parent::execute();

        $obj = null ;
        $dao = DAOFactory::getDAO($this->_objectType);
        if ($this->_id != null) {
            $obj = $dao->retrieveByPk($this->_id);
        }

        $typeId = $this->_request->getParameter("id_type");

        $this->_response->setAttribute("object", $obj);
        $this->_response->setAttribute("typeId", $typeId);

        $templateManager = TemplateManager::getInstance();
        $templateManager->addRemoteScript(
            'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        );
        $templateManager->addRemoteScript(
            'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'
        );

        switch(strtolower($this->_objectType)) {
        case 'sitecategory' :
        case 'allomediaentry' :
        case 'informationprofile' :
        case 'allomediasite' :
            $templateManager->addFileScript(
                'Applications/DefaultApp/Template/scripts/jquery.formPartsOrderer.js'
            );
            break;
        case 'fieldtype' :
            $structure = null ;
            if (!is_null($obj)) {
                $structure = $obj->getStructure();
            }
            $poDao = ObjectManager::getInstance()->
                                    getPrimitiveObjectDaoInstance(
                                        'JsonForm'
                                    );
            $jsonForm = $poDao->javascript($structure);
            break;
        case 'user' :
            $templateManager->addRemoteScript(
                './Scripts/jquery.validate.js'
            );
            break;
        case 'form' :
            $templateManager->addRemoteScript('./Scripts/ckeditor/ckeditor.js');
            $templateManager->addRemoteScript('./Scripts/ckeditor/adapters/jquery.js');
            $templateManager->addRemoteStyle('./Scripts/ckeditor/contents.css');
            
            $templateManager->addFileScript(
        		'Applications/DefaultApp/Template/scripts/jquery.formPartsOrderer.js'
            );
            $templateManager->addFileScript(
        		'Applications/DefaultApp/Template/scripts/jquery.multipleImages.js'
            );
            break;
        }

        $this->findForward(strtolower($this->_objectType));
    }
}
